//
// Created by 董一龙 on 2022/10/13.
//

#ifndef FQUEUE_RING_BUFFER_OPERATOR_H
#define FQUEUE_RING_BUFFER_OPERATOR_H

#include <sys/types.h>

namespace fqueue {

    class ring_buffer_operator {
    private:
#pragma pack(1)
        struct meta_info_t {
            // 大小端有问题
            static const uint64_t MAGIC_NUM = 0x12345678;
            uint64_t magic_num = MAGIC_NUM;
            // 1000
            int32_t front;
            int32_t rear;
            int32_t capacity;
            uint32_t size;
            uint32_t push_count;
            uint32_t pop_count;

            static bool is_valid(meta_info_t const*meta);
        };
#pragma pack()

        static meta_info_t *meta_info(void *buffer, size_t buffer_size);

        static uint8_t *data_buffer(void *buffer, size_t buffer_size, meta_info_t const* meta);

    public:
        static bool has_format(void *buffer, size_t buffer_size);

        static int format(void *buffer, size_t buffer_size);

        static size_t capacity(void *buffer, size_t buffer_size);

        static size_t size(void *buffer, size_t buffer_size);

        static int enqueue(void *buffer, size_t buffer_size, void const*input, size_t input_length);

        static int dequeue(void *buffer, size_t buffer_size, void *output, size_t output_length);

        static int clear(void *buffer, size_t buffer_size);
    };

} // fqueue

#endif //FQUEUE_RING_BUFFER_OPERATOR_H
